package yunwo.cn.yzstation.presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.BaomingAdapter;
import yunwo.cn.yzstation.adapter.HuidanAdapter;
import yunwo.cn.yzstation.api.IcostModel;
import yunwo.cn.yzstation.api.IcostView;
import yunwo.cn.yzstation.base.BasePresenter;
import yunwo.cn.yzstation.bean.DictionariesBean;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingFee;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingInfo;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingInfoDetail;
import yunwo.cn.yzstation.bean.PopupBean;
import yunwo.cn.yzstation.model.CostModel;
import yunwo.cn.yzstation.other.PickDialog;
import yunwo.cn.yzstation.view.activity.OrderActivity;
import yunwo.cn.yzstation.view.fregment.openorder.CostFragment;

/**
 * Created by lao on 2017/9/11.
 */

public class CostFragmentPersenter extends BasePresenter<IcostView> implements IcostModel {

    public SharedPreferences mDictionariesBean_sp;
    OrderActivity orderActivity;
    IcostView icostView;
    CostFragment costFragment;
    public CostModel mCostModel;
    public List<String> mReceiptPaylist;
    public HashMap<String, String> mReceiptPaymap;


    StaTrackingInfo orderInfoBean;
    StaTrackingFee staTrackingFee;
    StaTrackingInfoDetail staTrackingInfoDetail;

    public CostFragmentPersenter(OrderActivity orderActivity,
                                 IcostView icostView, CostFragment costFragment, StaTrackingInfo orderInfoBean,
                                 StaTrackingFee staTrackingFee, StaTrackingInfoDetail staTrackingInfoDetail) {

        this.orderInfoBean = orderInfoBean;
        this.staTrackingFee = staTrackingFee;
        this.staTrackingInfoDetail = staTrackingInfoDetail;


        this.orderActivity = orderActivity;
        this.costFragment = costFragment;
        this.icostView = icostView;
        mCostModel = new CostModel(orderActivity, this);
        mDictionariesBean_sp = orderActivity.getSharedPreferences("SP_DICTION", Context.MODE_PRIVATE);
        String zidian = mDictionariesBean_sp.getString("zidian", "");
        if (!zidian.equals("")) {
            Gson gson = new Gson();
            mReceiptPaylist = new ArrayList<>();
            mReceiptPaymap = new HashMap<>();
            DictionariesBean dictionariesBean = gson.fromJson(zidian, DictionariesBean.class);
            for (int i = 0; i < dictionariesBean.getData().size(); i++) {
                if (dictionariesBean.getData().get(i).getRemarks().equals("状态值")) {
                    mReceiptPaylist.add(dictionariesBean.getData().get(i).getDictName());
                    mReceiptPaymap.put(dictionariesBean.getData().get(i).getDictName(),dictionariesBean.getData().get(i).getDictValue());
                }
            }
        }
    }
    public String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);

        return str;
    }

    public void getBaoFei(String s ) {
        mCostModel.getBaoFei(s);

    }

    @Override
    public void getBaoFeiSuccess(int data) {
        icostView.getBaoFeiSuccess(data);
    }

    @Override
    public void getProcurationSuccess(int data) {
        icostView.getProcurationSuccess(data);
    }

    public void getProcuration(String s) {
        mCostModel.getProcuration(s);
    }


    public void initPayTypeDialogView(final PickDialog selectPayTypeDialog, final List<String> codeName,
                                      final HashMap<String, String> codeValue, TextView paymentTV, boolean ispay) {
        View view = selectPayTypeDialog.getView();
        ListView myListview = (ListView) view.findViewById(R.id.myListview);
        HuidanAdapter huidanAdapter = new HuidanAdapter(orderActivity, codeName, codeValue, selectPayTypeDialog, paymentTV, ispay,staTrackingInfoDetail);
        myListview.setAdapter(huidanAdapter);
        TextView cancelTV = (TextView) view.findViewById(R.id.cancelTV);
        cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPayTypeDialog.dismiss();
            }
        });
    }

    public void requestPopupCon(String s, String trim) {
        mCostModel.requestPopupCon(s,trim);
    }

    @Override
    public void requestPopupConSuccess(List<PopupBean.DataBean> popupBean) {
        icostView.requestPopupConSuccess(popupBean);
    }
}
